package cn.tedu.hospitalqd.service.impl;

import cn.tedu.hospitalqd.ex.ServiceException;
import cn.tedu.hospitalqd.mapper.GuideMapper;
import cn.tedu.hospitalqd.mapper.IntroMapper;
import cn.tedu.hospitalqd.pojo.dto.GuideAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.GuideUpdateDTO;
import cn.tedu.hospitalqd.pojo.dto.IntroAddNewDTO;
import cn.tedu.hospitalqd.pojo.dto.IntroUpdateDTO;
import cn.tedu.hospitalqd.pojo.entity.Guide;
import cn.tedu.hospitalqd.pojo.entity.Intro;
import cn.tedu.hospitalqd.pojo.vo.GuideListItemVO;
import cn.tedu.hospitalqd.pojo.vo.GuideStandarVO;
import cn.tedu.hospitalqd.pojo.vo.IntroListItemVO;
import cn.tedu.hospitalqd.pojo.vo.IntroStandarVO;
import cn.tedu.hospitalqd.service.GuideService;
import cn.tedu.hospitalqd.service.IntroService;
import cn.tedu.hospitalqd.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GuideServiceImpl implements GuideService {

    @Autowired
    private GuideMapper guideMapper;

    @Override
    public void addNew(GuideAddNewDTO guideAddNewDTO) {
        // 从参数对象中获取相册名称iTitle
        String title = guideAddNewDTO.getGTitle();
        // 调用albumMapper的int countByName(String name)根据名称执行统计
        int countByTitle = guideMapper.countByGTilteAndNotId(title);
        // 判断统计结果是否大于0
        if (countByTitle > 0) {
            // 是：抛出异常
            String message = "添加就医指南失败，医院简介名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建Album对象
        Guide guide = new Guide();
        // 调用BeanUtils.copyProperties()将参数对象的属性值复制到Album对象中
        BeanUtils.copyProperties(guideAddNewDTO, guide);
        // 调用albumMapper的int insert(Album album)方法将相册数据插入到数据库
        int rows = guideMapper.insert(guide);
        if (rows != 1) {
            String message = "添加就医指南失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        GuideStandarVO queryResult = guideMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除就医指南失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = guideMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除就医指南失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, GuideUpdateDTO guideUpdateDTO) {
        GuideStandarVO queryResult = guideMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "修改就医指南详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        String title = guideUpdateDTO.getGTitle();
        int count = guideMapper.countByGTilteAndNotId(title);
        if (count > 0) {
            String message = "修改就医指南详情失败，就医指南名称已经被占用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Guide guide = new Guide();
        BeanUtils.copyProperties(guideUpdateDTO, guide);
        guide.setId(id);
        int rows = guideMapper.update(guide);
        if (rows != 1) {
            String message = "修改就医指南详情失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public GuideStandarVO getStandardById(Long id) {
        GuideStandarVO queryResult = guideMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "获取就医指南详情失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public List<GuideListItemVO> list() {
        List<GuideListItemVO> list = guideMapper.list();
        return list;
    }

}
